package com.insigma.business.qrcode.intell._provider.impl;

import com.insigma.business.qrcode.common.converter.CompanyProviderConverter;
import com.insigma.business.qrcode.common.converter.UserProviderConverter;
import com.insigma.business.qrcode.common.template.BasePagingResponse;
import com.insigma.business.qrcode.common.template.area.ScenarioCodeEnum;
import com.insigma.business.qrcode.common.template.area.ServiceCallback;
import com.insigma.business.qrcode.common.util.BizCheckUtil;
import com.insigma.business.qrcode.common.util.ResponseUtils;
import com.insigma.business.qrcode.common.util.RpcContextUtil;
import com.insigma.business.qrcode.common.vo.BaseResponse;
import com.insigma.business.qrcode.common.vo.BaseSessionUser;
import com.insigma.business.qrcode.intell._provider.UserProvider;
import com.insigma.business.qrcode.intell.controller.BaseProviderImpl;
import com.insigma.business.qrcode.intell.dto.UserBSearcher;
import com.insigma.business.qrcode.intell.dto.UserDTO;
import com.insigma.business.qrcode.intell.dto.UserSearchRequest;
import com.insigma.business.qrcode.intell.entity.UserDO;
import com.insigma.business.qrcode.intell.service.UserService;
import com.insigma.business.qrcode.soldier.dto.CompanyDTO;
import com.insigma.business.qrcode.soldier.entity.CompanyDO;
import com.insigma.business.qrcode.soldier.request.BaseGetRequest;
import com.insigma.business.qrcode.soldier.service.CompanyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;
@Slf4j
@Service
@Component("userProvider")
public class UserProviderImpl extends BaseProviderImpl implements UserProvider {

    @Autowired
    private UserService userService;

    @Autowired
    private CompanyService companyService;

    @Override
    public BasePagingResponse<List<UserDTO>> searchUser(UserSearchRequest searchRequest) {
        final BasePagingResponse<List<UserDTO>> basePagingResponse = new BasePagingResponse<>();
        serviceTemplate.execute(basePagingResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(searchRequest, "查询分页User的请求参数不能为空");
            }

            @Override
            public void executeService() {
                final UserBSearcher userBSearcher = UserProviderConverter.convertRequestSO2Searcher(searchRequest);
                final List<UserDO> userDOList = userService.search(userBSearcher);
                final List<UserDTO> userDTOList = UserProviderConverter.convertResponseDOList2DTOList(userDOList);
                ResponseUtils.buildSuccessPagingResponse(basePagingResponse, userDTOList, searchRequest,userBSearcher.getTotalCount());
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.USER_SEARCH;
            }
        });

        return basePagingResponse;
    }

    @Override
    public BaseResponse<UserDTO> getUser(BaseGetRequest baseGetRequest) {
        return null;
    }

    @Override
    public CompanyDTO getCompanyInfo() {
        final BaseSessionUser baseSessionUser = RpcContextUtil.getBaseSessionUser();
        BizCheckUtil.checkNotNull(baseSessionUser, "用户非法");
        BizCheckUtil.checkNotNull(baseSessionUser.getUserId(), "获取单条Company的请求参数userId不能为空");

        final CompanyDO companyDO = companyService.getByUserId(baseSessionUser.getUserId());
        final CompanyDTO remoteObj = CompanyProviderConverter.convertResponseDO2DTO(companyDO);

        return remoteObj;
    }

    @Override
    public BaseResponse<UserDTO> getUserByAccount(String account) {
        return null;
    }
}
